package com.ruoyi.project.module.lys;

import com.ruoyi.common.core.exception.ServiceException;
import com.ruoyi.common.core.utils.StringUtils;
import com.ruoyi.common.core.utils.poi.ExcelUtil;
import com.ruoyi.common.core.web.controller.BaseController;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.common.core.web.page.TableDataInfo;
import com.ruoyi.common.log.annotation.Log;
import com.ruoyi.common.log.enums.BusinessType;
import com.ruoyi.common.security.annotation.RequiresPermissions;
import com.ruoyi.project.domain.*;
import com.ruoyi.project.service.ICxcyContractService;
import com.ruoyi.project.service.ICxcyTransformationService;
import com.ruoyi.project.service.ISysUserService;
import com.ruoyi.system.api.domain.SysUser;
import com.ruoyi.system.api.model.LoginUser;
import org.apache.poi.xwpf.converter.pdf.PdfConverter;
import org.apache.poi.xwpf.converter.pdf.PdfOptions;
import org.apache.poi.xwpf.usermodel.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

import static com.ruoyi.common.security.utils.SecurityUtils.getLoginUser;

/**
 * 成果转化申请Controller
 *
 * @author ruoyi
 * @date 2023-05-10
 */
@RestController
@RequestMapping("/lystransformation")
public class LysCxcyTransformationController extends BaseController
{
    @Autowired
    private ICxcyTransformationService cxcyTransformationService;
    @Autowired
    private ICxcyContractService cxcyContractService;
    @Autowired
    private ISysUserService userService;
    /**
     * 查询成果转化申请列表
     */
    @RequiresPermissions("project:lystransformation:list")
    @GetMapping("/list")
    public TableDataInfo list(CxcyTransformation cxcyTransformation)
    {  startPage();
        /*LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        List<CxcyTransformation> list = cxcyTransformationService.selectCxcyTransformationListById1("1");
        System.out.println(list.get(0).getFifthCollege()+"88");*/
        List<CxcyTransformation> list = cxcyTransformationService.selectCxcyTransformationList(cxcyTransformation);

        return getDataTable(list);
    }

    /**
     * 导出成果转化申请列表
     */
    @RequiresPermissions("project:lystransformation:export")
    @Log(title = "成果转化申请", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, CxcyTransformation cxcyTransformation)
    {
        List<CxcyTransformation> list = cxcyTransformationService.selectCxcyTransformationList(cxcyTransformation);
        ExcelUtil<CxcyTransformation> util = new ExcelUtil<CxcyTransformation>(CxcyTransformation.class);
        util.exportExcel(response, list, "成果转化申请数据");
    }

    /**
     * 获取成果转化申请详细信息
     */
    @RequiresPermissions("project:lystransformation:query")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return success(cxcyTransformationService.selectCxcyTransformationById(id));
    }

    /**
     * 新增成果转化申请
     */
    @RequiresPermissions("project:lystransformation:add")
    @Log(title = "成果转化申请", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody CxcyTransformation cxcyTransformation)
    {

        cxcyTransformation.setAuditState("1");
        return toAjax(cxcyTransformationService.insertCxcyTransformation(cxcyTransformation));
    }

    /**
     * 修改成果转化申请
     */
    @RequiresPermissions("project:lystransformation:edit")
    @Log(title = "成果转化申请", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody CxcyTransformation cxcyTransformation,CxcyBaseinfo cxcyBaseinfo)throws IOException
    {
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();

        Date d=new Date();
//        cxcyTransformation.setAuditTime(d);
        if(StringUtils.isBlank(cxcyTransformation.getFileId())){
            throw new ServiceException("档案编号不能为空！");
        }
        if(StringUtils.isBlank(cxcyTransformation.getNumberYears())){
            throw new ServiceException("年限不能为空！");
        }

        if(StringUtils.isBlank(cxcyTransformation.getTransformationMode())){
            throw new ServiceException("转化方式不能为空！");
        }
        if(StringUtils.isBlank(cxcyTransformation.getPatentSoftware())){
            throw new ServiceException("专利号/软著登记号不能为空！");
        }
        if(StringUtils.isBlank(cxcyTransformation.getObligee())){
            throw new ServiceException("权利人不能为空！");
        }
        if(StringUtils.isBlank(cxcyTransformation.getFundingStatement())){
            throw new ServiceException("资助说明不能为空！");
        }
        if(StringUtils.isBlank(cxcyTransformation.getTenureConfidentiality())){
            throw new ServiceException("权属/保密规定不能为空！");
        }
        if(StringUtils.isBlank(cxcyTransformation.getHistoricalTransaction())){
            throw new ServiceException("历史交易情况不能为空！");
        }
        if(StringUtils.isBlank(cxcyTransformation.getCorrelationCondition())){
            throw new ServiceException("关联情况不能为空！");
        }
        if(StringUtils.isBlank(cxcyTransformation.getLicenseMode())){
            throw new ServiceException("许可方式不能为空！");
        }
        if(StringUtils.isBlank(cxcyTransformation.getModeTransfer())){
            throw new ServiceException("转让方式不能为空！");
        }
        if(StringUtils.isBlank(cxcyTransformation.getPaymentAmount1())){
            throw new ServiceException("第一次（万元）方式不能为空！");
        }
        if(StringUtils.isNull(cxcyTransformation.getAcceptTime())){
            throw new ServiceException("受理时间不能为空！");
        }
        if(StringUtils.isNull(cxcyTransformation.getPaymentTime1())){
            throw new ServiceException("第一次付款时间不能为空！");
        }


//        cxcyTransformation.setAcceptTime(cxcyTransformation.getAcceptTime());
//        cxcyTransformation.setFileId(cxcyTransformation.getFileId());
//        cxcyTransformation.setNumberYears(cxcyTransformation.getNumberYears());
//        cxcyTransformation.setPaymentAmount1(cxcyTransformation.getPaymentAmount1());
//        cxcyTransformation.setPaymentTime1(cxcyTransformation.getPaymentTime1());
//        cxcyTransformation.setPaymentAmount2(cxcyTransformation.getPaymentAmount2());
//        cxcyTransformation.setPaymentTime2(cxcyTransformation.getPaymentTime2());
//        cxcyTransformation.setPaymentAmount3(cxcyTransformation.getPaymentAmount3());
//        cxcyTransformation.setPaymentTime3(cxcyTransformation.getPaymentTime3());
//        cxcyTransformation.setPaymentAmount4(cxcyTransformation.getPaymentAmount4());
//        cxcyTransformation.setPaymentTime4(cxcyTransformation.getPaymentTime4());
//        cxcyTransformation.setPayArrangement(cxcyTransformation.getPayArrangement());

        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");
        String acceptTime = dateFormat.format(cxcyTransformation.getAcceptTime());
//        if(!(cxcyTransformation.getAcceptTime().equals(null)&&cxcyTransformation.getFileId().equals(null))) {
        if (cxcyTransformation.getNote1().equals("1")) {

            //接收到前台传递过来的filepdf
            //把 filepdf的Wordpath字段设置为模板的路径
            cxcyTransformation.setPacketPath("D:\\ruoyi\\uploadPath\\lys\\01.渤海大学大学生创新创业成果转化审批表（许可、转让）.docx");
            //wordfilePath设置为模板的路径
            String wordfilePath = cxcyTransformation.getPacketPath();
            //声明fis，以字节流的形式读取文件，fis 代表一个 FileInputStream 对象，它指向要读取的 Word 文件。
            FileInputStream fis = new FileInputStream(wordfilePath);
            //创建一个 XWPFDocument 实例，该实例与指定的 Word 文件建立了连接，XWPFDocument 是一个用于处理 .docx 文件的类
            XWPFDocument document = new XWPFDocument(fis);
            //使用 for 循环遍历 XWPFDocument 中的所有段落

            for (XWPFParagraph paragraph : document.getParagraphs()) {
                //使用 XWPFParagraph 类中的 getParagraphText() 方法来获取当前处理的段落中包含的文本内容
                String paragraphText = paragraph.getParagraphText();
                // 判断该段落中是否包含需要替换的文本串，因为我们要替换name，project，money，具体根据需求更改
                if (paragraphText.contains("a") || paragraphText.contains("b") || paragraphText.contains("c") ||
                        paragraphText.contains("d") || paragraphText.contains("e") ||

                        paragraphText.contains("f") || paragraphText.contains("g") || paragraphText.contains("h") ||
                        paragraphText.contains("i") || paragraphText.contains("j") ||

                        paragraphText.contains("k") || paragraphText.contains("l") || paragraphText.contains("m") ||
                        paragraphText.contains("n") || paragraphText.contains("o") || paragraphText.contains("p") || paragraphText.contains("q")) {
                    //使用while是为了替换每一个，防止漏掉
                    boolean foundMatch = true;
                    while (foundMatch) {
                        foundMatch = false;
                        //使用 XWPFParagraph 类中的 getRuns() 方法获取当前处理的段落中包含的所有文本样式 Run
                        for (XWPFRun run : paragraph.getRuns()) {
                            //调用 getText(int pos) 方法可以获取该文本样式 Run 中从指定位置（pos）开始的所有文本信息，并将其作为一个字符串返回
                            String runText = run.getText(0);
                            //甲

                            if (runText != null && runText.contains("a")) {
                                runText = runText.replaceFirst("a", cxcyTransformation.getResultName());
                                run.setText(runText, 0);
                                foundMatch = true;
                            }
                            if (runText != null && runText.contains("b")) {
                                runText = runText.replaceFirst("b", cxcyTransformation.getFirstName());
                                run.setText(runText, 0);
                                foundMatch = true;
                            }
                            if (runText != null && runText.contains("c")) {
                                runText = runText.replaceFirst("c", cxcyTransformation.getCompletionUnit());
                                run.setText(runText, 0);
                                foundMatch = true;
                            }

                            if (runText != null && runText.contains("d")) {
                                runText = runText.replaceFirst("d", cxcyTransformation.getFirstName());
                                run.setText(runText, 0);
                                foundMatch = true;
                            }
                            if (runText != null && runText.contains("e")) {
                                runText = runText.replaceFirst("e", cxcyTransformation.getFirstPhone());
                                run.setText(runText, 0);
                                foundMatch = true;
                            }

//乙/
                            if (runText != null && runText.contains("f")) {
                                runText = runText.replaceFirst("f", acceptTime);
                                run.setText(runText, 0);
                                foundMatch = true;
                            }
                            if (runText != null && runText.contains("g")) {
                                runText = runText.replaceFirst("g", cxcyTransformation.getFileId());
                                run.setText(runText, 0);
                                foundMatch = true;
                            }


                        }
                    }
                }

            }
            for (XWPFTable table : document.getTables()) {
                for (XWPFTableRow row : table.getRows()) {
                    for (XWPFTableCell cell : row.getTableCells()) {
                        for (XWPFParagraph paragraph : cell.getParagraphs()) {
                            String paragraphText = paragraph.getText();
                            if (paragraphText.contains("a") || paragraphText.contains("b") || paragraphText.contains("c") ||
                                    paragraphText.contains("d") || paragraphText.contains("e") ||
                                    paragraphText.contains("f") || paragraphText.contains("g") || paragraphText.contains("h") ||
                                    paragraphText.contains("i") || paragraphText.contains("j") ||
                                    paragraphText.contains("k") || paragraphText.contains("l") || paragraphText.contains("m") ||
                                    paragraphText.contains("n") || paragraphText.contains("o") || paragraphText.contains("p") || paragraphText.contains("q") || paragraphText.contains("r") || paragraphText.contains("s") || paragraphText.contains("t")) {
                                boolean foundMatch = true;
                                while (foundMatch) {
                                    foundMatch = false;
                                    for (XWPFRun run : paragraph.getRuns()) {
                                        String runText = run.getText(0);
                                        if (runText != null && runText.contains("a")) {
                                            runText = runText.replaceFirst("a", cxcyTransformation.getResultName());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("j")) {
                                            runText = runText.replaceFirst("j", cxcyTransformation.getResultIntroduction());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("h")) {
                                            runText = runText.replaceFirst("h", cxcyTransformation.getPatentSoftware());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("i")) {
                                            runText = runText.replaceFirst("i", cxcyTransformation.getObligee());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("l")) {
                                            runText = runText.replaceFirst("l", cxcyTransformation.getFundingStatement());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("m")) {
                                            runText = runText.replaceFirst("m", cxcyTransformation.getContractName());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("n")) {
                                            runText = runText.replaceFirst("n", cxcyTransformation.getTenureConfidentiality());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("o")) {
                                            runText = runText.replaceFirst("o", cxcyTransformation.getHistoricalTransaction());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("p")) {
                                            runText = runText.replaceFirst("p", cxcyTransformation.getNumberYears());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("q")) {
                                            runText = runText.replaceFirst("q", cxcyTransformation.getEnterpriseName());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("r")) {
                                            runText = runText.replaceFirst("r", cxcyTransformation.getCorporateName());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("s")) {
                                            runText = runText.replaceFirst("s", cxcyTransformation.getCorporatePhone());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("t")) {
                                            runText = runText.replaceFirst("t", cxcyTransformation.getCorporateNumber());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }


                                    }
                                }
                            }
                        }
                    }
                }
            }
            //关闭FileInputStream
            fis.close();
            //设置path1为D:/ruoyi/uploadPath，因为若依默认存储为D:/ruoyi/uploadPath，可以根据配置文件进行更改
            String path = "D:/ruoyi/uploadPath";
            //拼接具体名称，按照时间命名，并且存储到相应位置
            String newwordFilePath = path +"/渤海大学大学生创新创业成果转化审批表"+ System.currentTimeMillis() + ".docx";
            FileOutputStream fos = new FileOutputStream(newwordFilePath);
            document.write(fos);
            fos.close();

            XWPFDocument doc = new XWPFDocument(new FileInputStream(newwordFilePath));// docx
            //转为pdf
            // 创建输出流，设置名称
            String newPdfFilePath = path + "/pdf_" + System.currentTimeMillis() + ".pdf";

            String outputPDFFiles = newPdfFilePath; // 输出的.pdf文件路径
            FileOutputStream foss = new FileOutputStream(outputPDFFiles);

            PdfOptions options = PdfOptions.create();

            // 将 .docx 转换为 .pdf
            PdfConverter.getInstance().convert(doc, foss, options);

            // 关闭输出流
            foss.close();

            System.out.println("转换完成");

            //因为前端不可以下载绝对路径，所以要拼接成http://127.0.0.1:9300/statics/***的形式，***为具体名称（D:/ruoyi/uploadPath后的名称）

            //设置wordpath字段
            int index = newwordFilePath.lastIndexOf("/");
            String q = newwordFilePath.substring(index);
//存放生成word
            cxcyTransformation.setPacketPath("http://210.47.178.218/statics" + q);
            //设置pdfpath字段
//            int index1 = newPdfFilePath.lastIndexOf("/");
//            String q1 = newPdfFilePath.substring(index1);
//
//            cxcyTransformation.setNote4("http://127.0.0.1:9300/statics" + q1);
        }else if(cxcyTransformation.getNote1().equals("2")){


            //接收到前台传递过来的filepdf
            //把 filepdf的Wordpath字段设置为模板的路径
            cxcyTransformation.setPacketPath("D:\\ruoyi\\uploadPath\\lys\\01.渤海大学大学生创新创业成果转化审批表（许可、转让）.docx");
            //wordfilePath设置为模板的路径
            String wordfilePath = cxcyTransformation.getPacketPath();
            //声明fis，以字节流的形式读取文件，fis 代表一个 FileInputStream 对象，它指向要读取的 Word 文件。
            FileInputStream fis = new FileInputStream(wordfilePath);
            //创建一个 XWPFDocument 实例，该实例与指定的 Word 文件建立了连接，XWPFDocument 是一个用于处理 .docx 文件的类
            XWPFDocument document = new XWPFDocument(fis);
            //使用 for 循环遍历 XWPFDocument 中的所有段落

            for (XWPFParagraph paragraph : document.getParagraphs()) {
                //使用 XWPFParagraph 类中的 getParagraphText() 方法来获取当前处理的段落中包含的文本内容
                String paragraphText = paragraph.getParagraphText();
                // 判断该段落中是否包含需要替换的文本串，因为我们要替换name，project，money，具体根据需求更改
                if (paragraphText.contains("a") || paragraphText.contains("b") || paragraphText.contains("c") ||
                        paragraphText.contains("d") || paragraphText.contains("e") ||

                        paragraphText.contains("f") || paragraphText.contains("g") || paragraphText.contains("h") ||
                        paragraphText.contains("i") || paragraphText.contains("j") ||

                        paragraphText.contains("k") || paragraphText.contains("l") || paragraphText.contains("m") ||
                        paragraphText.contains("n") || paragraphText.contains("o") || paragraphText.contains("p") || paragraphText.contains("q")) {
                    //使用while是为了替换每一个，防止漏掉
                    boolean foundMatch = true;
                    while (foundMatch) {
                        foundMatch = false;
                        //使用 XWPFParagraph 类中的 getRuns() 方法获取当前处理的段落中包含的所有文本样式 Run
                        for (XWPFRun run : paragraph.getRuns()) {
                            //调用 getText(int pos) 方法可以获取该文本样式 Run 中从指定位置（pos）开始的所有文本信息，并将其作为一个字符串返回
                            String runText = run.getText(0);
                            //甲

                            if (runText != null && runText.contains("a")) {
                                runText = runText.replaceFirst("a", cxcyTransformation.getResultName());
                                run.setText(runText, 0);
                                foundMatch = true;
                            }
                            if (runText != null && runText.contains("b")) {
                                runText = runText.replaceFirst("b", cxcyTransformation.getFirstName());
                                run.setText(runText, 0);
                                foundMatch = true;
                            }
                            if (runText != null && runText.contains("c")) {
                                runText = runText.replaceFirst("c", cxcyTransformation.getCompletionUnit());
                                run.setText(runText, 0);
                                foundMatch = true;
                            }

                            if (runText != null && runText.contains("d")) {
                                runText = runText.replaceFirst("d", cxcyTransformation.getFirstName());
                                run.setText(runText, 0);
                                foundMatch = true;
                            }
                            if (runText != null && runText.contains("e")) {
                                runText = runText.replaceFirst("e", cxcyTransformation.getFirstPhone());
                                run.setText(runText, 0);
                                foundMatch = true;
                            }

//乙/
                            if (runText != null && runText.contains("f")) {
                                runText = runText.replaceFirst("f", acceptTime);
                                run.setText(runText, 0);
                                foundMatch = true;
                            }
                            if (runText != null && runText.contains("g")) {
                                runText = runText.replaceFirst("g", cxcyTransformation.getFileId());
                                run.setText(runText, 0);
                                foundMatch = true;
                            }


                        }
                    }
                }

            }
            for (XWPFTable table : document.getTables()) {
                for (XWPFTableRow row : table.getRows()) {
                    for (XWPFTableCell cell : row.getTableCells()) {
                        for (XWPFParagraph paragraph : cell.getParagraphs()) {
                            String paragraphText = paragraph.getText();
                            if (paragraphText.contains("a") || paragraphText.contains("b") || paragraphText.contains("c") ||
                                    paragraphText.contains("d") || paragraphText.contains("e") ||
                                    paragraphText.contains("f") || paragraphText.contains("g") || paragraphText.contains("h") ||
                                    paragraphText.contains("i") || paragraphText.contains("j") ||
                                    paragraphText.contains("k") || paragraphText.contains("l") || paragraphText.contains("m") ||
                                    paragraphText.contains("n") || paragraphText.contains("o") || paragraphText.contains("p") || paragraphText.contains("q")||paragraphText.contains("r")||paragraphText.contains("s")||paragraphText.contains("t")) {
                                boolean foundMatch = true;
                                while (foundMatch) {
                                    foundMatch = false;
                                    for (XWPFRun run : paragraph.getRuns()) {
                                        String runText = run.getText(0);
                                        if (runText != null && runText.contains("a")) {
                                            runText = runText.replaceFirst("a", cxcyTransformation.getResultName());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("j")) {
                                            runText = runText.replaceFirst("j", cxcyTransformation.getResultIntroduction());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("h")) {
                                            runText = runText.replaceFirst("h", cxcyTransformation.getPatentSoftware());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("i")) {
                                            runText = runText.replaceFirst("i", cxcyTransformation.getObligee());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("l")) {
                                            runText = runText.replaceFirst("l", cxcyTransformation.getFundingStatement());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("m")) {
                                            runText = runText.replaceFirst("m", cxcyTransformation.getContractName());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("n")) {
                                            runText = runText.replaceFirst("n", cxcyTransformation.getTenureConfidentiality());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("o")) {
                                            runText = runText.replaceFirst("o", cxcyTransformation.getHistoricalTransaction());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("p")) {
                                            runText = runText.replaceFirst("p", cxcyTransformation.getNumberYears());
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("q")) {
                                            runText = runText.replaceFirst("q", "");
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("r")) {
                                            runText = runText.replaceFirst("r", "");
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("s")) {
                                            runText = runText.replaceFirst("s", "");
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }
                                        if (runText != null && runText.contains("t")) {
                                            runText = runText.replaceFirst("t", "");
                                            run.setText(runText, 0);
                                            foundMatch = true;
                                        }



                                    }
                                }
                            }
                        }
                    }
                }
            }
            //关闭FileInputStream
            fis.close();
            //设置path1为D:/ruoyi/uploadPath，因为若依默认存储为D:/ruoyi/uploadPath，可以根据配置文件进行更改
            String path = "D:/ruoyi/uploadPath";
            //拼接具体名称，按照时间命名，并且存储到相应位置
            String newwordFilePath = path +"/渤海大学大学生创新创业成果转化审批表"+ System.currentTimeMillis() + ".docx";
            FileOutputStream fos = new FileOutputStream(newwordFilePath);
            document.write(fos);
            fos.close();

            XWPFDocument doc = new XWPFDocument(new FileInputStream(newwordFilePath));// docx
            //转为pdf
            // 创建输出流，设置名称
            String newPdfFilePath = path + "/pdf_" + System.currentTimeMillis() + ".pdf";

            String outputPDFFiles = newPdfFilePath; // 输出的.pdf文件路径
            FileOutputStream foss = new FileOutputStream(outputPDFFiles);

            PdfOptions options = PdfOptions.create();

            // 将 .docx 转换为 .pdf
            PdfConverter.getInstance().convert(doc, foss, options);

            // 关闭输出流
            foss.close();

            System.out.println("转换完成");

            //因为前端不可以下载绝对路径，所以要拼接成http://127.0.0.1:9300/statics/***的形式，***为具体名称（D:/ruoyi/uploadPath后的名称）

            //设置wordpath字段
            int index = newwordFilePath.lastIndexOf("/");
            String q = newwordFilePath.substring(index);
//存放生成word
            cxcyTransformation.setPacketPath("http://210.47.178.218/statics" + q);
            //设置pdfpath字段
//            int index1 = newPdfFilePath.lastIndexOf("/");
//            String q1 = newPdfFilePath.substring(index1);
//
//            cxcyTransformation.setNote4("http://127.0.0.1:9300/statics" + q1);
        }
        cxcyTransformation.setApplyName(sysUser.getNickName());
        cxcyTransformation.setApplyNumber(sysUser.getUserName());
        cxcyTransformation.setApplyTime(d);

        // 执行更新
        cxcyTransformationService.updateCxcyTransformation(cxcyTransformation);
        System.out.println("11111111111111111111111111111111111");



//        cxcyTransformation.setAuditState("1");

        return toAjax(cxcyTransformationService.updateCxcyTransformation(cxcyTransformation));
    }

    /**
     * 删除成果转化申请
     */
    @RequiresPermissions("project:lystransformation:remove")
    @Log(title = "成果转化申请", businessType = BusinessType.DELETE)
    @DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(cxcyTransformationService.deleteCxcyTransformationByIds(ids));
    }

    /**
     * 审核成果转化申请--通过
     */
    @RequiresPermissions("project:lystransformation:revise")
    @Log(title = "成果转化申请", businessType = BusinessType.UPDATE)
    @PutMapping("/getshenhe")
    public AjaxResult revise(@RequestBody CxcyTransformation cxcyTransformation, CxcyContract cxcyContract)
    {



        // 获取当前登录用户
        LoginUser loginUser = getLoginUser();
        SysUser sysUser = loginUser.getSysUser();
        cxcyTransformation.setAuditName(sysUser.getNickName());
        cxcyTransformation.setAuditNumber(sysUser.getUserName());

        cxcyTransformation.setAuditState(cxcyTransformation.getAuditState());
        cxcyTransformationService.updateCxcyTransformation(cxcyTransformation);
        Date d=new Date();
        cxcyTransformation.setAuditTime(d);

//        cxcyTransformation.setAuditName(sysUser.getUserName());
        if (cxcyTransformation.getAuditState().equals("2")){
            LysCxcyTransformationWeChatAuditMassage.sendWeChatAuditMassage(cxcyTransformation);
            cxcyContract.setUserId(cxcyTransformation.getUserId());
            cxcyContract.setResultId(cxcyTransformation.getResultId());
            cxcyContract.setResultName(cxcyTransformation.getResultName());
            cxcyContract.setCompletionUnit(cxcyTransformation.getCompletionUnit());
            cxcyContract.setFirstName(cxcyTransformation.getFirstName());
            cxcyContract.setFirstNumber(cxcyTransformation.getFirstNumber());
            cxcyContract.setFirstPhone(cxcyTransformation.getFirstPhone());
            cxcyContract.setFirstIdnumber(cxcyTransformation.getFirstIdnumber());
            cxcyContract.setEnterpriseName(cxcyTransformation.getEnterpriseName());
            cxcyContract.setEnterpriseNumber(cxcyTransformation.getEnterpriseNumber());
            cxcyContract.setCorporateName(cxcyTransformation.getCorporateName());
            cxcyContract.setCorporatePhone(cxcyTransformation.getCorporatePhone());
            cxcyContract.setCorporateNumber(cxcyTransformation.getCorporateNumber());
            cxcyContractService.insertCxcyContract(cxcyContract);
        }else if(cxcyTransformation.getAuditState().equals("3")){

            LysCxcyTransformationWeChatAuditMassage.sendWeChatAuditMassage(cxcyTransformation);
        }


        return toAjax(cxcyTransformationService.updateCxcyTransformation(cxcyTransformation));
    }
    /**
     * 修改成果转化申请
     */
    @RequiresPermissions("project:lystransformation:edit1")
    @Log(title = "成果转化申请", businessType = BusinessType.UPDATE)
    @PutMapping("/a")
    public AjaxResult edit1(@RequestBody CxcyTransformation cxcyTransformation)
    {
        cxcyTransformation.setAuditState("1");
        ISysUser User = new ISysUser();
        User.setRemark("teacher");
        List<ISysUser> list2 = userService.selectMsgUserList(User);
        for (ISysUser user:list2) {
            LysCxcyTransformationWeChatMassage.sendWeChatMassage(cxcyTransformation,user);
        }

        return toAjax(cxcyTransformationService.updateCxcyTransformation(cxcyTransformation));
    }


}
